package com.ecube.maintenance.components.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

import com.ecube.maintenance.utils.Bog;


public class QuickSearchView extends android.widget.SearchView {
    private String lastQuery;
    private Context mContext;

    public QuickSearchView(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public QuickSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        this.mContext = context;
    }

    private void init() {
        int id = getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) findViewById(id);
        setIconifiedByDefault(true);
        setOnQueryTextListener(new OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                /*Intent intent = new Intent(Intent.ACTION_SEARCH);
                intent.putExtra(SearchManager.QUERY, getQuery().toString());
                intent.setClass(mContext, SearchListActivity.class);
                setIconified(true);
                mContext.startActivity(intent);*/
                Bog.toast("query:" + query);
                lastQuery = query;
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (!isIconified()) {
                    Bog.i("new Text Detected - " + newText);
                    lastQuery = newText;
                }

                return false;
            }
        });
    }

    @Override
    public void onActionViewCollapsed() {
        super.onActionViewCollapsed();
    }

    @Override
    public void onActionViewExpanded() {
        super.onActionViewExpanded();
        if (lastQuery != null) {
            setQuery(lastQuery, false);
        }
        //clearFocus();
    }

    public String getLastQuery() {
        return lastQuery;
    }

    public void setLastQuery(String lastQuery) {
        this.lastQuery = lastQuery;
    }


}